
#include "include/transmssion/tb_transmssion.h"
#include "include/non_standard_utility.h"
#include "include/paramnonstandard485.h"
#include <QDebug>

TB_Transmssion::TB_Transmssion( QObject* parent, ParamNonStandard485* param ) : TransmissionInter( parent, param ) {}

TB_Transmssion::~TB_Transmssion() {}

void TB_Transmssion::init( QList< QByteArray >* writeList, int state_ID ) {
    //获取485控制权限 01 06 00 B6 00 01 00 00
    QByteArray contorl( 6, 0x00 );
    //设定停止， 01 06 00 66 00 00 00 00
    QByteArray stop( 6, 0x00 );
    //设定速度, 01 06 00 56 00 00 00 00
    QByteArray speed( 6, 0x00 );
    contorl[ 0 ] = stop[ 0 ] = speed[ 0 ] = static_cast< uchar >( state_ID );
    contorl[ 0 ] = stop[ 1 ] = speed[ 1 ] = 0x06;
    contorl[ 3 ]                          = 0xB6;
    stop[ 3 ]                             = 0x66;
    speed[ 3 ]                            = 0x56;
    contorl[ 5 ]                          = 0x01;
    speed[ 4 ]                            = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed >> 8 );
    speed[ 5 ]                            = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed );

    QByteArray control_crc = Non_Standard_Utility::CRC16_ModBus( contorl, contorl.count() );
    contorl.append( control_crc );
    QByteArray speed_crc = Non_Standard_Utility::CRC16_ModBus( speed, speed.count() );
    speed.append( speed_crc );
    QByteArray stop_crc = Non_Standard_Utility::CRC16_ModBus( stop, stop.count() );
    stop.append( stop_crc );
    writeList->append( contorl );
    writeList->append( stop );
    writeList->append( speed );
}

void TB_Transmssion::onReadHandler( const QByteArray& readByte ) {
    if ( readByte.size() == 7 ) {
        if ( readByte.at( 1 ) == 0x03 && readByte.at( 4 ) != 0x00 ) {
            qDebug() << "DMK_Transmssion ReadHandler" << readByte.toHex();
            //错误码
            TransMissionErrorCode core    = TransMissionErrorCode( readByte.at( 4 ) );
            uchar                 stateID = readByte.at( 0 );
            emit                  sigTransmissionState( getTransMissionData( stateID, TransMissionState::Error, core ) );
        }
    }
    if ( readByte.size() == 8 ) {
        uchar stateID = readByte.at( 0 );
        if ( readByte.at( 1 ) == 0x06 && readByte.at( 3 ) == 0x66 ) {
            switch ( readByte.at( 5 ) ) {
            case 0:
                //启停
                emit sigTransmissionState( getTransMissionData( stateID, TransMissionState::Stop_ING, TransAction::Stop ) );
                break;
            case 0x01:
                //正反
                emit sigTransmissionState( getTransMissionData( stateID, TransMissionState::Start_Forward_ING, TransAction::Start_Forward ) );
                break;
            case 0x02:
                //正反
                emit sigTransmissionState( getTransMissionData( stateID, TransMissionState::Start_Reverse_ING, TransAction::Start_Reverse ) );
                break;

            default:
                break;
            }
        }
    }
}

void TB_Transmssion::onAddWriteData( QList< QByteArray >* writeList, const TransmissionInter::TransMissionData& transMissionData ) {
    switch ( static_cast< char >( transMissionData.trans_action ) ) {
    case TransAction::Start_Forward: {
        //正转（01 06 00 66 00 01）：0：停止 1:正转、2：反转
        QByteArray direction( 6, 0x00 );
        direction[ 0 ]           = static_cast< char >( transMissionData.device_address );
        direction[ 1 ]           = 0x06;
        direction[ 3 ]           = 0x66;
        direction[ 5 ]           = 0x01;
        QByteArray direction_crc = Non_Standard_Utility::CRC16_ModBus( direction, direction.count() );
        direction.append( direction_crc );
        writeList->append( direction );

    } break;
    case TransAction::Start_Reverse: {
        //反转（01 06 00 66 00 01）：0：停止 1:正转、2：反转
        QByteArray direction( 6, 0x00 );
        direction[ 0 ]           = static_cast< char >( transMissionData.device_address );
        direction[ 1 ]           = 0x06;
        direction[ 3 ]           = 0x66;
        direction[ 5 ]           = 0x02;
        QByteArray direction_crc = Non_Standard_Utility::CRC16_ModBus( direction, direction.count() );
        direction.append( direction_crc );
        writeList->append( direction );
        break;
    }
    case TransAction::Stop: {
        //停止（01 06 00 66 00 01）：0：停止 1:正转、2：反转
        QByteArray direction( 6, 0x00 );
        direction[ 0 ]           = static_cast< char >( transMissionData.device_address );
        direction[ 1 ]           = 0x06;
        direction[ 3 ]           = 0x66;
        QByteArray direction_crc = Non_Standard_Utility::CRC16_ModBus( direction, direction.count() );
        direction.append( direction_crc );
        writeList->append( direction );
        break;
    }

    default:
        break;
    }
}

void TB_Transmssion::onAddReadData( QList< QByteArray >* readList, int state_ID ) {
    //读取错误码01 03 00 76 00 01 00 00
    QByteArray readData( 6, 0x00 );
    readData[ 0 ]           = static_cast< char >( state_ID );
    readData[ 1 ]           = 0x03;
    readData[ 3 ]           = 0x76;
    readData[ 5 ]           = 0x01;
    QByteArray readData_crc = Non_Standard_Utility::CRC16_ModBus( readData, readData.count() );
    readData.append( readData_crc );
    readList->append( readData );
}
